﻿using System;
using System.Collections.Generic;
using System.Data;
using ExtensionHelper;

namespace ExcelImportHelper
{ 
    /// <summary>
    /// 
    /// </summary>
    public class AutomaticOperate
    {




        ExcelHelper helper = null;
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public List<T> ExcelToEntities<T>(string filePath,Action<T> action = null) where T : class, new()
        {
            List<T> list = new List<T>();
            try
            {
                string sheetName = "sheet1";//给默认值
                T model_class = new T();
                Type type = model_class.GetType();
                //判断类名称上有没有填写属性，如果有，则读取 SheetName
                object[] attribute_classes = type.GetCustomAttributes(true);
                int idx = attribute_classes.IndexOfAttribute(new ExcelSheetAttribute());
                ExcelSheetAttribute a = null;
                if (idx > -1)
                {
                    a = (ExcelSheetAttribute)attribute_classes[idx];
                    sheetName = a.SheetName;
                }
                helper = new ExcelHelper(filePath);
                DataTable dt = helper.ExcelToDataTable(sheetName, true);

                if (dt == null || dt.Rows.Count <= 0)
                {
                    //excel中没有数据
                    throw new Exception("excel中没有数据");
                }
                foreach (DataRow row in dt.Rows)
                {
                    T model = new T();
                    foreach (var p in type.GetProperties())
                    {
                        object[] attribute_fields = p.GetCustomAttributes(true);
                        int attribute_field_idx = attribute_fields.IndexOfAttribute(new ExcelFieldAttribute());
                        if (attribute_fields == null || attribute_field_idx < 0)
                        { 
                            //没有设置导入字段属性,则判断该字段是否在DT中有对应的列
                            if (dt.Columns.IndexOf(p.Name) > -1)
                            { 
                                if (p.PropertyType.Equal(typeof(float)))
                                {
                                    p.SetValue(model, row[p.Name].ToString().ToFloat());
                                }
                                if (p.PropertyType.Equal(typeof(DateTime)))
                                {
                                    p.SetValue(model, row[p.Name].ToString().ToDateTime(DateTime.Now));
                                }
                                if (p.PropertyType.Equal(typeof(string)))
                                {
                                    p.SetValue(model, row[p.Name].ToString());
                                }
                                if (p.PropertyType.Equal(typeof(int)))
                                {
                                    p.SetValue(model, row[p.Name].ToString().ToInt());
                                }
                                if (p.PropertyType.Equal(typeof(double)))
                                {
                                    p.SetValue(model, row[p.Name].ToString().ToDouble());
                                }
                            }
                        }
                        else
                        {
                           
                            if (attribute_field_idx > -1)
                            {
                                ExcelFieldAttribute excelField = null;
                                excelField = (ExcelFieldAttribute)attribute_fields[attribute_field_idx];
                                switch (excelField.DataType)
                                {
                                    case eFieldDataType.Int:
                                        p.SetValue(model, row[excelField.Name].ToString().ToInt(excelField.DefaultVal.ToInt()));
                                        break;
                                    case eFieldDataType.Float:
                                        p.SetValue(model, row[excelField.Name].ToString().ToFloat(excelField.DefaultVal.ToFloat()));
                                        break;
                                    case eFieldDataType.Double:
                                        p.SetValue(model, row[excelField.Name].ToString().ToDouble(excelField.DefaultVal.ToDouble()));
                                        break;
                                    case eFieldDataType.Datetime:
                                        p.SetValue(model, row[excelField.Name].ToString().ToDateTime(Convert.ToDateTime(excelField.DefaultVal)));
                                        break;
                                    case eFieldDataType.String:
                                        p.SetValue(model, row[excelField.Name].ToString());
                                        break;
                                    default:
                                        p.SetValue(model, row[excelField.Name].ToString());
                                        break;
                                }
                            }
                            else
                            {
                                
                            }
                        }
                        if (action != null)
                            action(model_class);


                    }
                    list.Add(model);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally {
                helper?.Dispose();
            }
        }



        
    }
}
